/*
 *  Copyright (C) 2005-2007 Renier Roth
 *  www.renier.de
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.renier.jkeepass.dialog;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import de.renier.jkeepass.Messages;
import de.renier.jkeepass.util.IconPool;
import de.renier.jkeepass.util.Utils;
import de.renier.jkeepass.components.SymbolComboBox;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.text.BadLocationException;


/**
 * TanInputDialog
 *
 * @author <a href="mailto:software@renier.de">Renier Roth</a>
 */
public class TanInputDialog extends JDialog {

  private static final long serialVersionUID = 1L;
  public static final int RESULT_CANCEL = 0;
  public static final int RESULT_OK = 1;
  private JPanel jContentPane = null;
  private JPanel buttonPanel = null;
  private JPanel infoPanel = null;
  private JButton submitButton = null;
  private JButton cancelButton = null;
  private JScrollPane jScrollPane = null;
  private JTextArea jTextArea = null;
  private JLabel jLabel = null;
  private int result = RESULT_CANCEL;
  private JLabel jLabel1 = null;
  private JFormattedTextField numberFormattedTextField = null;
  private SymbolComboBox symbolComboBox = null;
  private JLabel jLabel2 = null;
  private JTextField lineNumberTextField = null;
  private JLabel jLabel3 = null;
  private JFormattedTextField fieldSizeTextField = null;

  /**
   * @param owner
   */
  public TanInputDialog(Frame owner) {
    super(owner);
    initialize();
  }

  /**
   * This method initializes this
   * 
   * @return void
   */
  private void initialize() {
    this.setSize(410, 380);
    this.setTitle(Messages.getString("TanInputDialog.0")); //$NON-NLS-1$
    this.setContentPane(getJContentPane());
    this.setModal(true);
    this.setLocationRelativeTo(this.getOwner());
    getRootPane().setDefaultButton(getSubmitButton());
  }

  /**
   * This method initializes jContentPane
   * 
   * @return javax.swing.JPanel
   */
  private JPanel getJContentPane() {
    if (jContentPane == null) {
      jContentPane = new JPanel();
      jContentPane.setLayout(new BorderLayout());
      jContentPane.add(getButtonPanel(), BorderLayout.SOUTH);
      jContentPane.add(getInfoPanel(), BorderLayout.NORTH);
      jContentPane.add(getJScrollPane(), BorderLayout.CENTER);
    }
    return jContentPane;
  }

  /**
   * This method initializes buttonPanel	
   * 	
   * @return javax.swing.JPanel	
   */
  private JPanel getButtonPanel() {
    if (buttonPanel == null) {
      buttonPanel = new JPanel();
      buttonPanel.setLayout(new FlowLayout());
      buttonPanel.add(getSubmitButton(), null);
      buttonPanel.add(getCancelButton(), null);
    }
    return buttonPanel;
  }

  /**
   * This method initializes infoPanel	
   * 	
   * @return javax.swing.JPanel	
   */
  private JPanel getInfoPanel() {
    if (infoPanel == null) {
      GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
      gridBagConstraints5.fill = GridBagConstraints.VERTICAL;
      gridBagConstraints5.gridy = 2;
      gridBagConstraints5.weightx = 1.0;
      gridBagConstraints5.anchor = GridBagConstraints.WEST;
      gridBagConstraints5.insets = new Insets(0, 0, 0, 10);
      gridBagConstraints5.gridx = 1;
      GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
      gridBagConstraints4.gridx = 0;
      gridBagConstraints4.gridy = 2;
      jLabel3 = new JLabel();
      jLabel3.setText(Messages.getString("TanInputDialog.1")); //$NON-NLS-1$
      GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
      gridBagConstraints3.fill = GridBagConstraints.NONE;
      gridBagConstraints3.gridy = 3;
      gridBagConstraints3.weightx = 1.0;
      gridBagConstraints3.insets = new Insets(0, 0, 0, 10);
      gridBagConstraints3.anchor = GridBagConstraints.WEST;
      gridBagConstraints3.gridx = 1;
      GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
      gridBagConstraints12.gridx = 0;
      gridBagConstraints12.gridy = 3;
      jLabel2 = new JLabel();
      jLabel2.setText(Messages.getString("TanInputDialog.2")); //$NON-NLS-1$
      GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
      gridBagConstraints11.fill = GridBagConstraints.NONE;
      gridBagConstraints11.gridy = 1;
      gridBagConstraints11.weightx = 1.0;
      gridBagConstraints11.anchor = GridBagConstraints.WEST;
      gridBagConstraints11.gridx = 2;
      GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
      gridBagConstraints2.fill = GridBagConstraints.NONE;
      gridBagConstraints2.gridy = 1;
      gridBagConstraints2.weightx = 0.0D;
      gridBagConstraints2.anchor = GridBagConstraints.WEST;
      gridBagConstraints2.insets = new Insets(0, 0, 0, 10);
      gridBagConstraints2.gridx = 1;
      GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
      gridBagConstraints1.gridx = 0;
      gridBagConstraints1.insets = new Insets(0, 5, 0, 5);
      gridBagConstraints1.gridy = 1;
      jLabel1 = new JLabel();
      jLabel1.setText(Messages.getString("TanInputDialog.3")); //$NON-NLS-1$
      GridBagConstraints gridBagConstraints = new GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.ipadx = 102;
      gridBagConstraints.gridwidth = 3;
      gridBagConstraints.insets = new Insets(0, 5, 5, 5);
      gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
      gridBagConstraints.gridy = 0;
      jLabel = new JLabel();
      jLabel.setText(Messages.getString("TanInputDialog.4")); //$NON-NLS-1$
      jLabel.setVerticalAlignment(SwingConstants.TOP);
      jLabel.setVerticalTextPosition(SwingConstants.TOP);
      jLabel.setPreferredSize(new Dimension(300, 60));
      infoPanel = new JPanel();
      infoPanel.setLayout(new GridBagLayout());
      infoPanel.add(jLabel, gridBagConstraints);
      infoPanel.add(jLabel1, gridBagConstraints1);
      infoPanel.add(getNumberFormattedTextField(), gridBagConstraints2);
      infoPanel.add(getSymbolComboBox(), gridBagConstraints11);
      infoPanel.add(jLabel2, gridBagConstraints12);
      infoPanel.add(getLineNumberTextField(), gridBagConstraints3);
      infoPanel.add(jLabel3, gridBagConstraints4);
      infoPanel.add(getFieldSizeTextField(), gridBagConstraints5);
    }
    return infoPanel;
  }

  /**
   * This method initializes submitButton	
   * 	
   * @return javax.swing.JButton	
   */
  private JButton getSubmitButton() {
    if (submitButton == null) {
      submitButton = new JButton();
      submitButton.setText(Messages.getString("TanInputDialog.5")); //$NON-NLS-1$
      submitButton.setIcon(new ImageIcon(getClass().getResource("/org/javalobby/icons/20x20/NewRow.gif"))); //$NON-NLS-1$
      submitButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          result = RESULT_OK;
          dispose();
        }
      });
    }
    return submitButton;
  }

  /**
   * This method initializes cancelButton	
   * 	
   * @return javax.swing.JButton	
   */
  private JButton getCancelButton() {
    if (cancelButton == null) {
      cancelButton = new JButton();
      cancelButton.setText(Messages.getString("TanInputDialog.7")); //$NON-NLS-1$
      cancelButton.setIcon(new ImageIcon(getClass().getResource("/org/javalobby/icons/20x20/Delete.gif"))); //$NON-NLS-1$
      cancelButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          result = RESULT_CANCEL;
          dispose();
        }
      });
    }
    return cancelButton;
  }

  /**
   * This method initializes jScrollPane	
   * 	
   * @return javax.swing.JScrollPane	
   */
  private JScrollPane getJScrollPane() {
    if (jScrollPane == null) {
      jScrollPane = new JScrollPane();
      jScrollPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      jScrollPane.setViewportView(getJTextArea());
    }
    return jScrollPane;
  }

  /**
   * This method initializes jTextArea	
   * 	
   * @return javax.swing.JTextArea	
   */
  private JTextArea getJTextArea() {
    if (jTextArea == null) {
      jTextArea = new JTextArea();
      jTextArea.addCaretListener(new javax.swing.event.CaretListener() {
        public void caretUpdate(javax.swing.event.CaretEvent e) {
          try {
            lineNumberTextField.setText(String.valueOf(jTextArea.getLineOfOffset(e.getDot()) + 1));
          } catch (BadLocationException e1) {
            // nothing
          }
        }
      });
    }
    return jTextArea;
  }
  
  public String[] getTanArray() {
    String text = jTextArea.getText();
    if (!Utils.isEmpty(text)) {
      return text.split("\n| |,|;"); //$NON-NLS-1$
    }
    return null;
  }

  /**
   * getResult
   *
   * @return Returns the result.
   */
  public int getResult() {
    return result;
  }

  /**
   * This method initializes numberFormattedTextField	
   * 	
   * @return javax.swing.JTextField	
   */
  private JTextField getNumberFormattedTextField() {
    if (numberFormattedTextField == null) {
      numberFormattedTextField = new JFormattedTextField();
      numberFormattedTextField.setValue(new Integer(1));
      numberFormattedTextField.setPreferredSize(new Dimension(40, 19));
      numberFormattedTextField.setMinimumSize(new Dimension(40, 19));
    }
    return numberFormattedTextField;
  }

  /**
   * This method initializes symbolComboBox	
   * 	
   * @return de.renier.jkeepass.components.SymbolComboBox	
   */
  private SymbolComboBox getSymbolComboBox() {
    if (symbolComboBox == null) {
      symbolComboBox = new SymbolComboBox();
      symbolComboBox.setSelectedItem(IconPool.DEFAULT_TAN_ID);
    }
    return symbolComboBox;
  }
  
  /**
   * getOffset
   *
   * @return
   */
  public int getOffset() {
    Number num = (Number)numberFormattedTextField.getValue();
    return (num == null) ? 1 : num.intValue();
  }
  
  public int getFieldSize() {
    Number num = (Number)fieldSizeTextField.getValue();
    return (num == null) ? 0 : num.intValue();
  }
  
  /**
   * getSymbol
   *
   * @return
   */
  public String getSymbol() {
    return (String)symbolComboBox.getSelectedItem();
  }

  /**
   * This method initializes lineNumberTextField	
   * 	
   * @return javax.swing.JTextField	
   */
  private JTextField getLineNumberTextField() {
    if (lineNumberTextField == null) {
      lineNumberTextField = new JTextField();
      lineNumberTextField.setPreferredSize(new Dimension(40, 19));
      lineNumberTextField.setEditable(false);
      lineNumberTextField.setEnabled(false);
      lineNumberTextField.setMinimumSize(new Dimension(40, 19));
    }
    return lineNumberTextField;
  }

  /**
   * This method initializes fieldSizeTextField	
   * 	
   * @return javax.swing.JTextField	
   */
  private JTextField getFieldSizeTextField() {
    if (fieldSizeTextField == null) {
      fieldSizeTextField = new JFormattedTextField();
      fieldSizeTextField.setValue(new Integer(3));
      fieldSizeTextField.setPreferredSize(new Dimension(40, 19));
      fieldSizeTextField.setMinimumSize(new Dimension(40, 19));
      
    }
    return fieldSizeTextField;
  }

}  //  @jve:decl-index=0:visual-constraint="10,10"
